package com.codingapi.core.framework.utils;

import com.lorne.core.framework.utils.DateUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author 侯存路
 * @date 2018/4/10
 * @company codingApi
 * @description
 */
public class CodeUtils {

    private static String[] cards = new String[]{"aa", "ab", "ac", "ad", "ae"};

    private static String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
            "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0",
            "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    public static String getCode(int n) {
        String code = "";

        for (int i = 0; i < n; ++i) {
            code = code + getRandom10();
        }

        return code;
    }

    public static int getRandom10() {
        return (int) (Math.random() * 9.0D) + 1;
    }

    public static String getCode6() {
        return getCode(6);
    }

    public static String getDateNumSix() {
        return DateUtil.getCurrentDateTime() + getCode6();
    }

    public static int getRandomPrizeNum() {
        return new Random().nextInt(1000000) + 1;
    }

    private static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        return format.format(date);
    }

    private static String getStringCode(int len) {
        Random random = new Random();
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < len; i++) {
            res.append(chars[random.nextInt(chars.length)]);
        }
        return res.toString();
    }

    public static String getCardBatchId(int type) {
        String start = cards[type - 1];
        return start +
                formatDate(new Date()) +
                getStringCode(3);
    }

    public static String getCardOnlyId() {
        return getStringCode(7);
    }
}
